package services

import (
	"beego-admin/formvalidate"
	"beego-admin/global"
	"beego-admin/models"
	"beego-admin/utils/page"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/cache"
	_ "github.com/astaxie/beego/cache/redis"
	"github.com/beego/beego/v2/client/orm"
	"net/url"
	"strconv"
	"time"
)

// PassagersService struct
type PassagersService struct {
	BaseService
}

// GetPaginateData 通过分页获取user
func (us *PassagersService) GetPaginateData(listRows int, params url.Values) ([]*models.Passagers, page.Pagination) {
	//搜索、查询字段赋值
	us.SearchField = append(us.SearchField, new(models.Passagers).SearchField()...)

	var passagers []*models.Passagers
	o := orm.NewOrm().QueryTable(new(models.Passagers))
	_, err := us.PaginateAndScopeWhere(o, listRows, params).All(&passagers)
	if err != nil {
		return nil, us.Pagination
	}
	return passagers, us.Pagination
}

// Create 新增用户
func (*PassagersService) Create(form *formvalidate.PassagersForm) int {
	passagers := models.Passagers{
		Authorname:    form.Authorname,
		Nickname:    form.Nickname,
		Title:      form.Title,
		Description: form.Description,
		Status:      int8(form.Status),
		CreateTime:  int(time.Now().Unix()),
		UpdateTime:  int(time.Now().Unix()),
	}

	if form.Avatar != "" {
		passagers.Avatar = form.Avatar
	}

	id, err := orm.NewOrm().Insert(&passagers)

	if err == nil {


		bytes, _ := json.Marshal(map[string]string{"key":"chatengine", "conn":global.BA_CONFIG.Redis.Addr, "dbNum":global.BA_CONFIG.Redis.DB, "password":global.BA_CONFIG.Redis.Password})

		cache_conn, err := cache.NewCache("redis", string(bytes))

		if err != nil {fmt.Println(err)}

		dataCache, _ := json.Marshal(passagers)

		err = cache_conn.Put("passagers"+strconv.Itoa(int(id)), string(dataCache), time.Second*10000)

		if err != nil {

			fmt.Println("数据存取出错，错误为：",err)//后面处理回滚mysql

			return 0

		}else{

			return int(id)

		}

	}else{

		return 0

	}

}

// GetUserById 根据id获取一条user数据
func (*PassagersService) GetPassagersById(id int) *models.Passagers {
	o := orm.NewOrm()
	passagers := models.Passagers{Id: id}
	err := o.Read(&passagers)
	if err != nil {
		return nil
	}
	return &passagers
}

// Update 更新用户
func (*PassagersService) Update(form *formvalidate.PassagersForm) int {
	o := orm.NewOrm()
	passagers := models.Passagers{Id: form.Id}
	if o.Read(&passagers) == nil {

		////判断密码是否相等
		//if user.Password != form.Password {
		//	newPasswordForHash, err := utils.PasswordHash(form.Password)
		//	if err == nil {
		//		user.Password = base64.StdEncoding.EncodeToString([]byte(newPasswordForHash))
		//	}
		//}

		passagers.Authorname = form.Authorname
		passagers.Nickname = form.Nickname
		passagers.Title = form.Title
		passagers.Description = form.Description
		passagers.Status = int8(form.Status)
		passagers.UpdateTime = int(time.Now().Unix())

		if form.Avatar != "" {
			passagers.Avatar = form.Avatar
		}
		num, err := o.Update(&passagers)

		if err == nil {
			return int(num)
		}
		return 0
	}
	return 0
}

// Enable 启用
func (*PassagersService) Enable(ids []int) int {
	num, err := orm.NewOrm().QueryTable(new(models.Passagers)).Filter("id__in", ids).Update(orm.Params{
		"status": 1,
	})
	if err == nil {
		return int(num)
	}
	return 0
}

// Disable 禁用
func (*PassagersService) Disable(ids []int) int {
	num, err := orm.NewOrm().QueryTable(new(models.Passagers)).Filter("id__in", ids).Update(orm.Params{
		"status": 0,
	})
	if err == nil {
		return int(num)
	}
	return 0
}

// Del 删除
func (*PassagersService) Del(ids []int) int {
	count, err := orm.NewOrm().QueryTable(new(models.Passagers)).Filter("id__in", ids).Delete()
	if err == nil {
		return int(count)
	}
	return 0
}

// GetExportData 获取导出数据
func (us *PassagersService) GetExportData(params url.Values) []*models.Passagers {
	//搜索、查询字段赋值
	us.SearchField = append(us.SearchField, new(models.Passagers).SearchField()...)
	var passagers []*models.Passagers
	o := orm.NewOrm().QueryTable(new(models.Passagers))
	_, err := us.ScopeWhere(o, params).All(&passagers)
	if err != nil {
		return nil
	}
	return passagers
}
